<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Dcat\Admin\Traits\ModelTree;

/**
 * App\Models\ArticleCategory
 *
 * @property int $id
 * @property int $parent_id 父级ID
 * @property string $title 分类名称
 * @property int $order 排序
 * @property string $seo_title seo标题
 * @property string $seo_keywords seo关键词
 * @property string $seo_description seo摘要
 * @property \Illuminate\Support\Carbon|null $created_at 添加时间
 * @property \Illuminate\Support\Carbon|null $updated_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory ordered(string $direction = 'asc')
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory query()
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereSeoDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereSeoKeywords($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereSeoTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ArticleCategory whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Article[] $articles
 * @property-read int|null $articles_count
 * @property-read \Illuminate\Database\Eloquent\Collection|ArticleCategory[] $children
 * @property-read int|null $children_count
 * @property-read ArticleCategory|null $parent
 */
class ArticleCategory extends Model
{
    use ModelTree;
    use HasDateTimeFormatter;

    protected $table = 'article_category';

    public function selectOptions(\Closure $closure = null, $rootText = null)
    {
        $options = (new static())->withQuery($closure)
            ->buildSelectOptions();

        return collect($options)
            ->prepend($rootText, 0)
            ->all();
    }

    public function articles()
    {
        return $this->hasMany(Article::class, 'category_id', 'id');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'parent_id', 'id');
    }

    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id', 'id');
    }

    //获取顶级ID
    public function getTopParentId()
    {
        $category = self::pluck('parent_id', 'id');
        $id = $this->id;
        while ($category[$id]) {
            $id = $category[$id];
        }
        return $id;
    }

    public function getNav()
    {
        $top_category_id = $this->getTopParentId();
        $articles_data = self::find($top_category_id)->articles;
        $categories_data = self::find($top_category_id)->children;
        return $this->handle_category_data($articles_data, $categories_data);
    }

    public function handle_category_data($articles_data, $categories_data)
    {
        $articles = [];
        $categories = [];
        foreach ($articles_data as $k => $article) {
            $articles[$k]['title'] = $article->title;
            $articles[$k]['sort'] = $article->sort;
            $articles[$k]['url'] = route('articles.show', ['article' => $article->id]);
        }
        foreach ($categories_data as $k => $category) {
            $categories[$k]['title'] = $category->title;
            $categories[$k]['sort'] = $category->order;
            $categories[$k]['url'] = route('articles.category', ['article_category' => $category->id]);
        }
        $item = array_merge($articles, $categories);
        array_multisort(array_column($item, 'sort'), SORT_ASC, $item);
        return $item;
    }
}
